package com.mall.order.biz.handler;

import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CartProductDto;
import com.mall.order.utils.GlobalIdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 * 初始化订单 生成订单
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    RedissonClient client;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        Order order = new Order();
        //插入数据库的Order类的初始化
        //todo 发号器

        String orderId = null;
        try {
            orderId = new GlobalIdGeneratorUtil().getNextSeq("order",1);
        } catch (IOException e) {
            throw new RuntimeException("订单编号生成失败");
        }
        order.setOrderId(orderId);
        order.setUserId(createOrderContext.getUserId());
        order.setBuyerNick(createOrderContext.getBuyerNickName());
        order.setPayment(createOrderContext.getOrderTotal());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);

        //插入order表。order表不包含购买的商品信息，只包含订单信息
        orderMapper.insert(order);

        //OrderItem 订单中的商品，购物车中的下单的那些挪到订单商品表
        List<CartProductDto> cartProductDtoList = createOrderContext.getCartProductDtoList();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            //todo 发号器
            String orderItemID = null;
            try {
                orderItemID = new GlobalIdGeneratorUtil().getNextSeq("orderItem",1);
            } catch (IOException e) {
                throw new RuntimeException("订单商品编号生成失败");
            }
            orderItem.setId(orderItemID);
            orderItem.setOrderId(orderId);
            orderItem.setItemId(cartProductDto.getProductId());

            Integer num = cartProductDto.getProductNum().intValue();
            orderItem.setNum(num);

            Double price = cartProductDto.getSalePrice().doubleValue();
            orderItem.setPrice(price);

            BigDecimal total = cartProductDto.getSalePrice().multiply(new BigDecimal(cartProductDto.getProductNum()));
            orderItem.setTotalFee(total.doubleValue());

            orderItem.setPicPath(cartProductDto.getProductImg());
            orderItem.setTitle(cartProductDto.getProductName());

            orderItem.setStatus(1); //1 库存已锁定 2 库存已释放 3 库存减扣成功
            orderItemMapper.insert(orderItem);

        }

        //CreateOrderContext继承自AbsTransHandlerContext
        createOrderContext.setOrderId(orderId);


        return true;
    }
}
